﻿Public Class OrgChart
    Inherits System.Web.UI.Page

	Protected Sub gvOffical_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOffical.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOffical.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			Me.txtSearch.Text = oj.Text
			Me.mpu.Hide()
		End If
	End Sub

	Protected Sub gvOffical_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOffical.PageIndexChanging
		Me.gvOffical.DataSource = Me.Session("sponsorsearch")
		Me.gvOffical.PageIndex = e.NewPageIndex
		Me.gvOffical.DataBind()
		Me.mpu.Show()
	End Sub

	Protected Sub btnSponsorSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSponsorSearch.Click
		If HttpContext.Current.User.IsInRole("admin") Then
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Staff(Me.txtSearchSponsor.Text)
		Else
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Member(Me.txtSearchSponsor.Text, GlobalApp.Authen.offcier_id)
		End If
		Me.gvOffical.PageIndex = 0
		Me.gvOffical.DataBind()
		Me.mpu.Show()
		Me.Session("sponsorsearch") = Me.gvOffical.DataSource
	End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			If GlobalApp.Authen IsNot Nothing Then
				If String.IsNullOrEmpty(Me.Request.QueryString("key")) And String.IsNullOrEmpty(Me.Request.QueryString("keyz")) Then
					Me.ucOrgChart1.LoadChart(GlobalApp.Authen.offcier_id, String.Empty)
				ElseIf Not String.IsNullOrEmpty(Me.Request.QueryString("keyz")) Then
					Me.ucOrgChart1.LoadChart(Me.Request.QueryString("keyz"))
				Else
					If Not String.IsNullOrEmpty(Me.Request.QueryString("keys")) Then
						Me.ucOrgChart1.LoadChart(GlobalApp.Authen.offcier_id, Me.Request.QueryString("keys"))
					Else
						Me.ucOrgChart1.LoadChart(Me.Request.QueryString("key"), String.Empty)
					End If
				End If
		End If
		End If

		If GlobalApp.Authen.role_id.Equals("1") Then 'ไม่ใช้ staff ไม่ show
			Me.imgSponsorPopup.Visible = False
			Me.Panel2.Visible = False
		End If
    End Sub

	Private Sub ucOrgChart1_Alert(ByVal msg As String) Handles ucOrgChart1.Alert
		Me.Master.Alert(msg)
	End Sub

	Private Sub ucOrgChart1_WriteToolTip(ByVal divtag As String) Handles ucOrgChart1.WriteToolTip
		Me.ltrDivToolTips.Text &= divtag
	End Sub

	Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click
		If Not String.IsNullOrEmpty(Me.txtSearch.Text) Then
			If GlobalApp.Authen.role_id.Equals("1") Then
				Response.Redirect("~/OrgChart.aspx?key=" & GlobalApp.Authen.offcier_id & "&keys=" & Me.txtSearch.Text, False)
			Else
				Response.Redirect("~/OrgChart.aspx?keyz=" & Me.txtSearch.Text, False)
			End If
		End If
	End Sub

	Protected Sub btnRoot_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnRoot.Click
		Response.Redirect("~/OrgChart.aspx", False)
	End Sub

	Protected Sub btnPLv_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnPLv.Click

		If Me.ucOrgChart1.FirstNod IsNot Nothing Then

			If Me.ucOrgChart1.FirstNod.officer_id.Equals(GlobalApp.Authen.offcier_id) Then
				Response.Redirect("~/OrgChart.aspx", False)
				GlobalApp.DepositMessage = "ท่านไม่สามารถดูแผนผังองค์กรที่อยู่เหนือท่านได้"
				Exit Sub
			End If

			Dim strUpline As String = BL.Officer.GetUpline(Me.ucOrgChart1.FirstNod.officer_id)
			If Not String.IsNullOrEmpty(strUpline) Then
				Response.Redirect("~/OrgChart.aspx?key=" & GlobalApp.Authen.offcier_id & "&keys=" & strUpline, False)
			Else
				Me.Master.Alert("ไม่พบแผนผังองค์กรก่อนหน้า")
			End If

		Else
			Me.Master.Alert("ไม่สามารถแสดงแผนผังองค์กรก่อนหน้าได้")
		End If


	End Sub
End Class